﻿Imports System.Configuration
Imports System.ComponentModel

Namespace Config

    ''' <summary>
    ''' The queue settings section
    ''' </summary>
    ''' <remarks></remarks>
    Public Class QueueSection
        Inherits ConfigurationSection

        <ConfigurationProperty("", IsDefaultCollection:=True)> _
        <ConfigurationCollection(GetType(QueueSetting))> _
        Public ReadOnly Property Queues() As QueueSettingsCollection
            Get
                Return DirectCast(MyBase.Item(""), QueueSettingsCollection)
            End Get
        End Property

    End Class

    ''' <summary>
    ''' The queue settings
    ''' </summary>
    ''' <remarks></remarks>
    Public NotInheritable Class QueueSetting
        Inherits ConfigurationElement

        Private Const NameProperty As String = "name"
        Private Const QueueIDProperty As String = "queueid"
        Private Const TempQueueIDProperty As String = "tempqueueid"
        Private Const TempQueueAgeProperty As String = "tempqueueage"
        Private Const SendVerificationMailProperty As String = "sendverificationmail"
        Private Const IgnoreListProperty As String = "ignorelist"
        Private Const MoveIgnoredProperty As String = "moveignored"
        Private Const VerificationMailFromProperty As String = "verificationmail-from"
        Private Const VerificationMailBccProperty As String = "verificationmail-bcc"
        Private Const VerificationMailSubjectProperty As String = "verificationmail-subject"
        Private Const VerificationMailTemplateProperty As String = "verificationmail-template"
        Private Const VerificationMailUseHtmlProperty As String = "verificationmail-usehtml"
        Private Const VerificationMailUrlProperty As String = "verificationmail-url"
        Private Const CreateContactProperty As String = "createcontact"
        Private Const AllowOnlyToProperty As String = "allowonlyto"
        Private Const AllowOnlyToReplyProperty As String = "allowonlyto-reply"
        Private Const AllowOnlyToNotifyProperty As String = "allowonlyto-notify"
        Private Const AllowOnlyToFromProperty As String = "allowonlyto-from"
        Private Const AllowOnlyToTemplateProperty As String = "allowonlyto-template"
        Private Const AllowOnlyToSubjectProperty As String = "allowonlyto-subject"
        Private Const AllowOnlyToUseHtmlProperty As String = "allowonlyto-usehtml"
        Private Const MatchCaseTicketNumberProperty As String = "matchcaseticketnumber"
        Private Const SendCaseMailProperty As String = "sendcasemail"
        Private Const CaseMailFromProperty As String = "casemail-from"
        Private Const CaseMailFromTypeProperty As String = "casemail-fromtype"
        Private Const CaseMailNotifyProperty As String = "casemail-notify"
        Private Const CaseMailSubjectProperty As String = "casemail-subject"
        Private Const CaseMailTemplateProperty As String = "casemail-template"
        Private Const CaseMailUseHtmlProperty As String = "casemail-usehtml"
        Private Const UpdateCaseDescriptionProperty As String = "updatecasedescription"
        Private Const IntervalProperty As String = "interval"
        Private Const ResolveParentProperty As String = "resolveparent"
        Private Const ResolveParentNotifyProperty As String = "resolveparent-notify"
        Private Const ResolveParentFromProperty As String = "resolveparent-from"
        Private Const SendCaseNotificationProperty As String = "sendcasenotificationmail"
        Private Const CaseNotificationFromProperty As String = "casenotification-from"
        Private Const CaseNotificationToProperty As String = "casenotification-to"
        Private Const CaseNotificationSubjectProperty As String = "casenotification-subject"
        Private Const CaseNotificationTemplateProperty As String = "casenotification-template"
        Private Const CaseNotificationUseHtmlProperty As String = "casenotification-usehtml"
        Private Const CaseAttributesProperty As String = "caseattributes"
        Private Const RemoveRepliesFromQueueProperty As String = "removerepliesfromqueue"
        Private Const MapCaseToAccountProperty As String = "mapcasetoaccount"
        Private Const SetResponsibleContactProperty As String = "setresponsiblecontact"
        Private Const ParentAccountMissingProperty As String = "parentaccountmissing"

        Public Sub New()
        End Sub

        Public Sub New(ByVal elementName As String)
            Name = elementName
        End Sub

        <ConfigurationProperty(NameProperty, IsKey:=True, IsRequired:=True)> _
        Public Property Name() As String
            Get
                Return DirectCast(Me(NameProperty), String)
            End Get
            Set(ByVal value As String)
                Me(NameProperty) = value
            End Set
        End Property

        <ConfigurationProperty(IntervalProperty, IsRequired:=True)> _
        Public Property Interval() As Integer
            Get
                Return DirectCast(Me(IntervalProperty), Integer)
            End Get
            Set(ByVal value As Integer)
                Me(IntervalProperty) = value
            End Set
        End Property

        <ConfigurationProperty(SendVerificationMailProperty, DefaultValue:=False)> _
        Public Property SendVerificationMail() As Boolean
            Get
                Return DirectCast(Me(SendVerificationMailProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(SendVerificationMailProperty) = value
            End Set
        End Property

        <ConfigurationProperty(CreateContactProperty, DefaultValue:=True)> _
        Public Property CreateContact() As Boolean
            Get
                Return DirectCast(Me(CreateContactProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(CreateContactProperty) = value
            End Set
        End Property

        <ConfigurationProperty(ResolveParentProperty, DefaultValue:=False, IsRequired:=False)> _
        Public Property ResolveParent() As Boolean
            Get
                Return DirectCast(Me(ResolveParentProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(ResolveParentProperty) = value
            End Set
        End Property

        <ConfigurationProperty(ResolveParentNotifyProperty, DefaultValue:="", IsRequired:=False)> _
        Public Property ResolveParentNotify() As String
            Get
                Return DirectCast(Me(ResolveParentNotifyProperty), String)
            End Get
            Set(ByVal value As String)
                Me(ResolveParentNotifyProperty) = value
            End Set
        End Property

        <ConfigurationProperty(ResolveParentFromProperty, DefaultValue:="", IsRequired:=False)> _
        Public Property ResolveParentFrom() As String
            Get
                Return DirectCast(Me(ResolveParentFromProperty), String)
            End Get
            Set(ByVal value As String)
                Me(ResolveParentFromProperty) = value
            End Set
        End Property

        <ConfigurationProperty(AllowOnlyToProperty, DefaultValue:=False, IsRequired:=False)> _
        Public Property AllowOnlyTo() As Boolean
            Get
                Return DirectCast(Me(AllowOnlyToProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(AllowOnlyToProperty) = value
            End Set
        End Property

        <ConfigurationProperty(IgnoreListProperty, IsRequired:=False)> _
        Public Property IgnoreList() As String
            Get
                Return DirectCast(Me(IgnoreListProperty), String)
            End Get
            Set(ByVal value As String)
                Me(IgnoreListProperty) = value
            End Set
        End Property

        <ConfigurationProperty(MoveIgnoredProperty, DefaultValue:=False, IsRequired:=False)> _
        Public Property MoveIgnored() As Boolean
            Get
                Return DirectCast(Me(MoveIgnoredProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(MoveIgnoredProperty) = value
            End Set
        End Property

        <ConfigurationProperty(AllowOnlyToNotifyProperty, IsRequired:=False)> _
        Public Property AllowOnlyToNotify() As String
            Get
                Return DirectCast(Me(AllowOnlyToNotifyProperty), String)
            End Get
            Set(ByVal value As String)
                Me(AllowOnlyToNotifyProperty) = value
            End Set
        End Property

        <ConfigurationProperty(AllowOnlyToFromProperty, IsRequired:=False)> _
        Public Property AllowOnlyToFrom() As String
            Get
                Return DirectCast(Me(AllowOnlyToFromProperty), String)
            End Get
            Set(ByVal value As String)
                Me(AllowOnlyToFromProperty) = value
            End Set
        End Property

        <ConfigurationProperty(AllowOnlyToReplyProperty, IsRequired:=False, DefaultValue:=True)> _
        Public Property AllowOnlyToReply() As Boolean
            Get
                Return DirectCast(Me(AllowOnlyToReplyProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(AllowOnlyToReplyProperty) = value
            End Set
        End Property

        <ConfigurationProperty(AllowOnlyToUseHtmlProperty, IsRequired:=False, DefaultValue:=True)> _
        Public Property AllowOnlyToUseHtml() As Boolean
            Get
                Return DirectCast(Me(AllowOnlyToUseHtmlProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(AllowOnlyToUseHtmlProperty) = value
            End Set
        End Property

        <ConfigurationProperty(AllowOnlyToSubjectProperty, IsRequired:=False)> _
        Public Property AllowOnlyToSubject() As String
            Get
                Return DirectCast(Me(AllowOnlyToSubjectProperty), String)
            End Get
            Set(ByVal value As String)
                Me(AllowOnlyToSubjectProperty) = value
            End Set
        End Property

        <ConfigurationProperty(AllowOnlyToTemplateProperty, IsRequired:=False)> _
        Public Property AllowOnlyToTemplate() As String
            Get
                Return DirectCast(Me(AllowOnlyToTemplateProperty), String)
            End Get
            Set(ByVal value As String)
                Me(AllowOnlyToTemplateProperty) = value
            End Set
        End Property

        <ConfigurationProperty(VerificationMailFromProperty, IsRequired:=False)> _
        Public Property VerificationMailFrom() As String
            Get
                Return DirectCast(Me(VerificationMailFromProperty), String)
            End Get
            Set(ByVal value As String)
                Me(VerificationMailFromProperty) = value
            End Set
        End Property

        <ConfigurationProperty(VerificationMailBccProperty, IsRequired:=False)> _
        Public Property VerificationMailBcc() As String
            Get
                Return DirectCast(Me(VerificationMailBccProperty), String)
            End Get
            Set(ByVal value As String)
                Me(VerificationMailBccProperty) = value
            End Set
        End Property

        <ConfigurationProperty(VerificationMailSubjectProperty, IsRequired:=False)> _
        Public Property VerificationMailSubject() As String
            Get
                Return DirectCast(Me(VerificationMailSubjectProperty), String)
            End Get
            Set(ByVal value As String)
                Me(VerificationMailSubjectProperty) = value
            End Set
        End Property

        <ConfigurationProperty(VerificationMailTemplateProperty, IsRequired:=False)> _
        Public Property VerificationMailTemplate() As String
            Get
                Return DirectCast(Me(VerificationMailTemplateProperty), String)
            End Get
            Set(ByVal value As String)
                Me(VerificationMailTemplateProperty) = value
            End Set
        End Property

        <ConfigurationProperty(VerificationMailUrlProperty, IsRequired:=False)> _
        Public Property VerificationMailUrl() As String
            Get
                Return DirectCast(Me(VerificationMailUrlProperty), String)
            End Get
            Set(ByVal value As String)
                Me(VerificationMailUrlProperty) = value
            End Set
        End Property

        <ConfigurationProperty(VerificationMailUseHtmlProperty, IsRequired:=False, DefaultValue:=True)> _
        Public Property VerificationMailUseHtml() As Boolean
            Get
                Return DirectCast(Me(VerificationMailUseHtmlProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(VerificationMailUseHtmlProperty) = value
            End Set
        End Property

        <ConfigurationProperty(SendCaseMailProperty, DefaultValue:=True)> _
        Public Property SendCaseMail() As Boolean
            Get
                Return DirectCast(Me(SendCaseMailProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(SendCaseMailProperty) = value
            End Set
        End Property

        <ConfigurationProperty(QueueIDProperty, IsRequired:=True)> _
        Public Property QueueID() As String
            Get
                Return DirectCast(Me(QueueIDProperty), String)
            End Get
            Set(ByVal value As String)
                Me(QueueIDProperty) = value
            End Set
        End Property

        <ConfigurationProperty(TempQueueIDProperty, IsRequired:=False)> _
        Public Property TempQueueID() As String
            Get
                Return DirectCast(Me(TempQueueIDProperty), String)
            End Get
            Set(ByVal value As String)
                Me(TempQueueIDProperty) = value
            End Set
        End Property

        <ConfigurationProperty(TempQueueAgeProperty, IsRequired:=False, DefaultValue:=0)> _
        Public Property TempQueueAge() As Integer
            Get
                Return DirectCast(Me(TempQueueAgeProperty), Integer)
            End Get
            Set(ByVal value As Integer)
                Me(TempQueueAgeProperty) = value
            End Set
        End Property

        <ConfigurationProperty(MatchCaseTicketNumberProperty, IsRequired:=False, DefaultValue:=False)> _
        Public Property MatchCaseTicketNumber() As Boolean
            Get
                Return DirectCast(Me(MatchCaseTicketNumberProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(MatchCaseTicketNumberProperty) = value
            End Set
        End Property

        <ConfigurationProperty(CaseMailFromProperty, IsRequired:=False)> _
        Public Property CaseMailFrom() As String
            Get
                Return DirectCast(Me(CaseMailFromProperty), String)
            End Get
            Set(ByVal value As String)
                Me(CaseMailFromProperty) = value
            End Set
        End Property

        <ConfigurationProperty(CaseMailFromTypeProperty, IsRequired:=False)> _
        Public Property CaseMailFromType() As String
            Get
                Return DirectCast(Me(CaseMailFromTypeProperty), String)
            End Get
            Set(ByVal value As String)
                Me(CaseMailFromTypeProperty) = value
            End Set
        End Property

        <ConfigurationProperty(CaseMailNotifyProperty, IsRequired:=False)> _
        Public Property CaseMailNotify() As String
            Get
                Return DirectCast(Me(CaseMailNotifyProperty), String)
            End Get
            Set(ByVal value As String)
                Me(CaseMailNotifyProperty) = value
            End Set
        End Property

        <ConfigurationProperty(CaseMailSubjectProperty, IsRequired:=False)> _
        Public Property CaseMailSubject() As String
            Get
                Return DirectCast(Me(CaseMailSubjectProperty), String)
            End Get
            Set(ByVal value As String)
                Me(CaseMailSubjectProperty) = value
            End Set
        End Property

        <ConfigurationProperty(CaseMailTemplateProperty, IsRequired:=False)> _
        Public Property CaseMailTemplate() As String
            Get
                Return DirectCast(Me(CaseMailTemplateProperty), String)
            End Get
            Set(ByVal value As String)
                Me(CaseMailTemplateProperty) = value
            End Set
        End Property

        <ConfigurationProperty(CaseMailUseHtmlProperty, IsRequired:=False, DefaultValue:=True)> _
        Public Property CaseMailUseHtml() As Boolean
            Get
                Return DirectCast(Me(CaseMailUseHtmlProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(CaseMailUseHtmlProperty) = value
            End Set
        End Property

        <ConfigurationProperty(UpdateCaseDescriptionProperty, DefaultValue:=False)> _
        Public Property UpdateCaseDescription() As Boolean
            Get
                Return DirectCast(Me(UpdateCaseDescriptionProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(UpdateCaseDescriptionProperty) = value
            End Set
        End Property

        <ConfigurationProperty(SendCaseNotificationProperty, DefaultValue:=False)> _
        Public Property SendCaseNotification() As Boolean
            Get
                Return DirectCast(Me(SendCaseNotificationProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(SendCaseNotificationProperty) = value
            End Set
        End Property

        <ConfigurationProperty(CaseNotificationFromProperty, IsRequired:=False)> _
        Public Property CaseNotificationFrom() As String
            Get
                Return DirectCast(Me(CaseNotificationFromProperty), String)
            End Get
            Set(ByVal value As String)
                Me(CaseNotificationFromProperty) = value
            End Set
        End Property

        <ConfigurationProperty(CaseNotificationToProperty, IsRequired:=False)> _
        Public Property CaseNotificationTo() As String
            Get
                Return DirectCast(Me(CaseNotificationToProperty), String)
            End Get
            Set(ByVal value As String)
                Me(CaseNotificationToProperty) = value
            End Set
        End Property

        <ConfigurationProperty(CaseNotificationSubjectProperty, IsRequired:=False)> _
        Public Property CaseNotificationSubject() As String
            Get
                Return DirectCast(Me(CaseNotificationSubjectProperty), String)
            End Get
            Set(ByVal value As String)
                Me(CaseNotificationSubjectProperty) = value
            End Set
        End Property

        <ConfigurationProperty(CaseNotificationTemplateProperty, IsRequired:=False)> _
        Public Property CaseNotificationTemplate() As String
            Get
                Return DirectCast(Me(CaseNotificationTemplateProperty), String)
            End Get
            Set(ByVal value As String)
                Me(CaseNotificationTemplateProperty) = value
            End Set
        End Property

        <ConfigurationProperty(CaseNotificationUseHtmlProperty, DefaultValue:=False)> _
        Public Property CaseNotificationUseHtml() As Boolean
            Get
                Return DirectCast(Me(CaseNotificationUseHtmlProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(CaseNotificationUseHtmlProperty) = value
            End Set
        End Property

        <ConfigurationProperty(CaseAttributesProperty, IsRequired:=False)> _
        Public Property CaseAttributes() As String
            Get
                Return DirectCast(Me(CaseAttributesProperty), String)
            End Get
            Set(ByVal value As String)
                Me(CaseAttributesProperty) = value
            End Set
        End Property

        <ConfigurationProperty(RemoveRepliesFromQueueProperty, DefaultValue:=False, IsRequired:=False)> _
        Public Property RemoveRepliesFromQueue() As Boolean
            Get
                Return DirectCast(Me(RemoveRepliesFromQueueProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(RemoveRepliesFromQueueProperty) = value
            End Set
        End Property

        <ConfigurationProperty(MapCaseToAccountProperty, DefaultValue:=False, IsRequired:=False)> _
        Public Property MapCaseToAccount() As Boolean
            Get
                Return DirectCast(Me(MapCaseToAccountProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(MapCaseToAccountProperty) = value
            End Set
        End Property

        <ConfigurationProperty(SetResponsibleContactProperty, DefaultValue:=False, IsRequired:=False)> _
        Public Property SetResponsibleContact() As Boolean
            Get
                Return DirectCast(Me(SetResponsibleContactProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(SetResponsibleContactProperty) = value
            End Set
        End Property

        <ConfigurationProperty(ParentAccountMissingProperty, DefaultValue:=False, IsRequired:=False)> _
        Public Property ParentAccountMissing() As Boolean
            Get
                Return DirectCast(Me(ParentAccountMissingProperty), Boolean)
            End Get
            Set(ByVal value As Boolean)
                Me(ParentAccountMissingProperty) = value
            End Set
        End Property

    End Class

    Public NotInheritable Class QueueSettingsCollection
        Inherits ConfigurationElementCollection(Of QueueSetting)

        Public Sub New()
            MyBase.New(StringComparer.CurrentCultureIgnoreCase)
        End Sub

        Protected Overloads Overrides Function CreateNewElement() As ConfigurationElement
            Return New QueueSetting
        End Function

        Protected Overrides Function CreateNewElement(ByVal elementName As String) As ConfigurationElement
            Return New QueueSetting(elementName)
        End Function

        Protected Overrides Function GetElementKey(ByVal element As ConfigurationElement) As Object
            Return DirectCast(element, QueueSetting).Name
        End Function

    End Class

    Public MustInherit Class ConfigurationElementCollection(Of T As ConfigurationElement)
        Inherits ConfigurationElementCollection

        Protected Sub New()

        End Sub

        Protected Sub New(ByVal comparer As IComparer)
            MyBase.New(comparer)
        End Sub

        Default Public Overloads Property Item(ByVal index As Integer) As T
            Get
                Return DirectCast(Me(index), T)
            End Get
            Set(ByVal value As T)
                Me(index) = value
            End Set
        End Property

        Default Public Overloads Property Item(ByVal name As String) As T
            Get
                Return DirectCast(Me(name), T)
            End Get
            Set(ByVal value As T)
                Me(name) = value
            End Set
        End Property

        Public Sub Add(ByVal settings As T)
            BaseAdd(settings)
        End Sub

        Public Sub Clear()
            BaseClear()
        End Sub

        Public Sub Remove(ByVal name As String)
            BaseRemove(name)
        End Sub

        Public Sub Remove(ByVal settings As T)
            BaseRemove(GetElementKey(settings))
        End Sub

        Public Sub RemoveAt(ByVal index As Integer)
            BaseRemoveAt(index)
        End Sub

        Public Function IndexOf(ByVal settings As T) As Integer
            Return BaseIndexOf(settings)
        End Function

        Protected Overrides Sub BaseAdd(ByVal index As Integer, ByVal element As ConfigurationElement)
            MyBase.BaseAdd(index, element)
        End Sub

    End Class

End Namespace